package com.wngbms.simple;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

@Slf4j
public class UDPServer {

    public static void main(String[] args) throws IOException, InterruptedException {

        InetAddress inetAddress = InetAddress.getByName("192.168.31.79");
        DatagramSocket socket = new DatagramSocket(8888, inetAddress);
        byte[] data = new byte[1024];

        log.info("server stared...");

        DatagramPacket packet = new DatagramPacket(data, data.length);
        socket.receive(packet);

        String info = new String(data, 0, packet.getLength());
        log.info("server recv:[{}]", info);


        InetAddress address = packet.getAddress();
        int port = packet.getPort();

        byte[] bytes = "hello client".getBytes();
        DatagramPacket packet1 = new DatagramPacket(bytes, bytes.length, address, port);
        socket.send(packet1);

        Thread.sleep(10000);

        socket.close();
    }

}
